package main

import (
	"fmt"
	"log"
	"net"
	"net/http"
)

func main() {
	http.HandleFunc("/hijack", func(w http.ResponseWriter, r *http.Request) {
		hj, ok := w.(http.Hijacker)
		if !ok {
			http.Error(w, "webserver doesn't support hijacking", http.StatusInternalServerError)
		}

		con, b, err := hj.Hijack()
		if err != nil {
			http.Error(w, err.Error(), http.StatusInternalServerError)
		}
		defer func(con net.Conn) {
			err := con.Close()
			if err != nil {
				log.Fatalln(err)
			}
		}(con)
		b.WriteString("Now we're speaking raw TCP. Say hi: ")

		b.Flush()

		readString, err := b.ReadString('\n')
		if err != nil {
			log.Printf("error reading string: %v", err)
		}

		fmt.Fprintf(b, "You said: %q\\nBye.\\n", readString)
		b.Flush()
	})

	//http.ListenAndServe(":8000", nil)
}
